﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;

namespace TS_XY_Train_Repaire.UserCon.Locomotive.TrainRepair.Work.WorkPub
{
    public partial class uc_Locomotive_TrainRepair_Work_WorkPub_Day_Pub : DevExpress.XtraEditors.XtraForm
    {
        public string _BMWID = "";
        public uc_Locomotive_TrainRepair_Work_WorkPub_Day_Pub()
        {
            InitializeComponent();
        }

        private void uc_Locomotive_TrainRepair_Work_WorkPub_Day_Pub_Load(object sender, EventArgs e)
        {
            _LoadStepsList();
        }
        private void _LoadStepsList()
        {
            string strSQL = "select f_id,f_state,f_pstime,f_petime,(select f_ttname+f_name+' '+CONVERT(varchar(100), f_stime, 23) from TS_TR_BU_Work_Main where f_id=TS_TR_BU_Work_Main_Day.f_bmwid)+' '+f_name as f_name from TS_TR_BU_Work_Main_Day where f_id=" + _BMWID;
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            SqlConnection conn_s = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            while (read.Read())
            {
                this.Text = read["f_name"].ToString();
                if (read["f_state"].ToString() == "0" || read["f_state"].ToString() == "-1")
                {
                    gp_Edit.Enabled = true;
                    bt_Pub.Enabled = true;
                    bt_Del.Enabled = false;
                }
                else
                {
                    gp_Edit.Enabled = true;
                    bt_Pub.Enabled = false;
                    bt_Del.Enabled = true;
                }

                de_S.Text = read["f_pstime"].ToString();
                de_E.Text = read["f_petime"].ToString();
            }
            xtt_NoPub.Nodes.Clear();
            TR_PS_Work_Main_List _tv = new TR_PS_Work_Main_List();
            ArrayList NList = new ArrayList();
            read.Close();
            strSQL = "select f_id,f_name,f_bmwdid,convert(nvarchar(16),f_pstime,120) as f_pstime,convert(nvarchar(16),f_petime,120) as f_petime from TS_TR_BU_Work_Main_Steps where f_del=0 and f_bmwdid=" + _BMWID;
            comm = new SqlCommand(strSQL, conn);
            read = comm.ExecuteReader();
            while (read.Read())
            {
                _tv = new TR_PS_Work_Main_List();
                _tv.f_id = "S" + read["f_id"].ToString();
                _tv.f_parent = "D" + read["f_bmwdid"].ToString();
                _tv.f_name = read["f_name"].ToString();
                _tv.f_stime = read["f_pstime"].ToString();
                _tv.f_etime = read["f_petime"].ToString();
                NList.Add(_tv);
            }
            read.Close();
            //strSQL = "select f_id,f_name,f_bmwsid,convert(nvarchar(16),f_stime,120) as f_stime,convert(nvarchar(16),f_etime,120) as f_etime,f_state from TS_TR_BU_Work_Main_Group where f_del=0 and f_bmwdid=" + _BMWID;
            //comm = new SqlCommand(strSQL, conn);
            //read = comm.ExecuteReader();
            //while (read.Read())
            //{
            //    _tv = new TR_PS_Work_Main_List();
            //    _tv.f_id = "G" + read["f_id"].ToString();
            //    _tv.f_parent = "S" + read["f_bmwsid"].ToString();
            //    _tv.f_name = read["f_name"].ToString();
            //    _tv.f_stime = read["f_stime"].ToString();
            //    _tv.f_etime = read["f_etime"].ToString();
            //    NList.Add(_tv);
            //}
            //read.Close();
            //strSQL = "select f_id,f_name+f_brief as f_name,f_bmwgid,convert(nvarchar(16),f_ctime,120) as f_stime,' ' as f_etime,f_state from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_bmwdid=" + _BMWID;
            //comm = new SqlCommand(strSQL, conn);
            //read = comm.ExecuteReader();
            //while (read.Read())
            //{
            //    _tv = new TR_PS_Work_Main_List();
            //    _tv.f_id = "P" + read["f_id"].ToString();
            //    _tv.f_parent = "G" + read["f_bmwgid"].ToString();
            //    _tv.f_name = read["f_name"].ToString();
            //    _tv.f_stime = read["f_stime"].ToString();
            //    _tv.f_etime = read["f_etime"].ToString();
            //    NList.Add(_tv);
            //}
            //read.Close();
            conn.Close();
            conn.Dispose();
            xtt_NoPub.DataSource = NList;
            xtt_NoPub.ExpandAll();
        }
        private void uc_Locomotive_TrainRepair_Work_WorkPub_Day_Pub_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void bt_Edit_Click(object sender, EventArgs e)
        {
            string strSQL = "update TS_TR_BU_Work_Main_Day set f_pstime='" + de_S.Text.ToString() + "', f_petime='" + de_E.Text.ToString() + "',f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_id=" + _BMWID ;
            strSQL += ";update TS_TR_BU_Work_Main_Steps set f_pstime=(select CONVERT(varchar(100),f_pstime, 23) from TS_TR_BU_Work_Main_Day where f_id=TS_TR_BU_Work_Main_Steps.f_bmwdid) + ' ' +CONVERT(varchar(100), f_pstime, 8),f_petime=(select CONVERT(varchar(100),f_petime, 23) from TS_TR_BU_Work_Main_Day where f_id=TS_TR_BU_Work_Main_Steps.f_bmwdid) + ' ' +CONVERT(varchar(100), f_petime, 8) where f_bmwdid=" + _BMWID;
            strSQL += ";update TS_TR_BU_Work_Main_Group set f_pstime=(select CONVERT(varchar(100),f_pstime, 23) from TS_TR_BU_Work_Main_Day where f_id=TS_TR_BU_Work_Main_Group.f_bmwdid) + ' ' +CONVERT(varchar(100), f_pstime, 8),f_petime=(select CONVERT(varchar(100),f_petime, 23) from TS_TR_BU_Work_Main_Day where f_id=TS_TR_BU_Work_Main_Group.f_bmwdid) + ' ' +CONVERT(varchar(100), f_petime, 8) where f_bmwdid=" + _BMWID;
            TS_Win_Class.MySqlCom(strSQL);
        }

        private void bt_Pub_Click(object sender, EventArgs e)
        {
            string strSQL = "update TS_TR_BU_Work_Main_Day set f_pstime='" + de_S.Text.ToString() + "', f_petime='" + de_E.Text.ToString() + "',f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_id=" + _BMWID;
            strSQL += ";update TS_TR_BU_Work_Main_Steps set f_pstime=(select CONVERT(varchar(100),f_pstime, 23) from TS_TR_BU_Work_Main_Day where f_id=TS_TR_BU_Work_Main_Steps.f_bmwdid) + ' ' +CONVERT(varchar(100), f_pstime, 8),f_petime=(select CONVERT(varchar(100),f_petime, 23) from TS_TR_BU_Work_Main_Day where f_id=TS_TR_BU_Work_Main_Steps.f_bmwdid) + ' ' +CONVERT(varchar(100), f_petime, 8) where f_bmwdid=" + _BMWID;
            strSQL += ";update TS_TR_BU_Work_Main_Group set f_pstime=(select CONVERT(varchar(100),f_pstime, 23) from TS_TR_BU_Work_Main_Day where f_id=TS_TR_BU_Work_Main_Group.f_bmwdid) + ' ' +CONVERT(varchar(100), f_pstime, 8),f_petime=(select CONVERT(varchar(100),f_petime, 23) from TS_TR_BU_Work_Main_Day where f_id=TS_TR_BU_Work_Main_Group.f_bmwdid) + ' ' +CONVERT(varchar(100), f_petime, 8) where f_bmwdid=" + _BMWID;
            TS_Win_Class.MySqlCom(strSQL);
            strSQL += "update TS_TR_BU_Work_Main_Day set f_stime=getdate() where f_id=" + _BMWID+"  and f_stime is null";
            TS_Win_Class.MySqlCom(strSQL);
            strSQL = "update TS_TR_BU_Work_Main_Day set f_state=1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_id=" + _BMWID;
            TS_Win_Class.MySqlCom(strSQL);
            strSQL = "insert into TS_TR_BU_Work_Main_Relation(f_rtype,f_rid,f_ftype,f_fid,f_workerid,f_cstuffid,f_ctime) values(1," + _BMWID + ",2," + cb_1.Tag.ToString() + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex + ",getdate())";
            string _ID = TS_Win_Class.MySqlComGetID(strSQL);
            strSQL = "insert into TS_S_T_P_Voice_Announce(f_vid,f_tablename,f_string,f_funcid,f_type,f_workerid) select " + _ID + " as f_vid,'TS_TR_BU_Work_Main_Relation' as f_tablename,'" + Create_Voice_String(cb_1.Tag.ToString(), " ") + "' as f_string," + cb_1.Tag.ToString() + " as f_funcid,2 as f_type," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_workerid from TS_TR_BU_Work_Main_Day where f_del=0 and f_id=" + _BMWID;
            TS_Win_Class.MySqlCom(strSQL);
            this.DialogResult = DialogResult.OK;
        }
        private string Create_Voice_String(string _ID, string _Type)
        {
            string _str = "";
            string strSQL = "select '机车号'+(select f_ttname+f_name from TS_TR_BU_Work_Main where f_id=TS_TR_BU_Work_Main_Day.f_bmwid)+','+f_name as f_name,f_stime,f_etime from TS_TR_BU_Work_Main_Day where f_id=" + _BMWID;
            string[] _Result;
            _Result = TS_Win_Class.GetSQlFieldValue(strSQL, "f_name,f_stime,f_etime".Split(','));
            _str = _Result[0];
            _str += ",计划开始作业时间:" + _Result[1];
            _str += ",计划作业结束时间:" + _Result[2];
            strSQL = "select f_name from TS_S_T_P_Department where f_id in (" + cb_1.Tag.ToString() + ")";
            _str += ",任务内容已经发布，";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            if (read.HasRows)
            {
                _str += "作业部门为：";
                while (read.Read())
                {
                    _str += read["f_name"].ToString();
                }
            }
            else
            {
                _str += "尚未指定作业班组！";
            }
            _str = _Type + _str;
            read.Close();
            read.Dispose();
            comm.Dispose();
            conn.Close();
            conn.Dispose();
            return _str;
        }

        private void bt_Del_Click(object sender, EventArgs e)
        {
            string strSQL = "update TS_TR_BU_Work_Main_Day set f_state=0,f_time=getdate(),f_stime=null,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_id="+_BMWID;
            strSQL += ";update TS_TR_BU_Work_Main_Steps set f_state=0,f_time=getdate(),f_stime=null,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_bmwdid=" + _BMWID;
            strSQL += ";update TS_TR_BU_Work_Main_Group set f_state=0,f_time=getdate(),f_stime=null,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_del=0 and f_bmwdid=" + _BMWID;
            strSQL += ";update TS_TR_BU_Work_Main_Group_WorkPlace set f_state=-1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_del=0 and f_bmwdid=" + _BMWID;
            strSQL += ";update TS_TR_BU_Work_Main_Relation set f_del=1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_rtype=1 and f_rid in (" + _BMWID + ") and f_del=0";
            strSQL += ";update TS_TR_BU_Work_Main_Resource set f_del=1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_maintype=1 and f_mainid=" + _BMWID + " and f_del=0";
            strSQL += ";update TS_TR_BU_Work_Main_Relation set f_del=1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_rtype=2 and f_rid in (select f_id from TS_TR_BU_Work_Main_Steps where f_del=0 and f_bmwdid in (" + _BMWID + ")) and f_del=0";
            strSQL += ";update TS_TR_BU_Work_Main_Resource set f_del=1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_maintype=2 and f_mainid in (select f_id from TS_TR_BU_Work_Main_Steps where f_del=0 and f_bmwdid in (" + _BMWID + ")) and f_del=0";
            strSQL += ";update TS_TR_BU_Work_Main_Relation set f_del=1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_rtype=3 and f_rid in (select f_id from TS_TR_BU_Work_Main_Group where f_del=0 and f_bmwdid in (" + _BMWID + ")) and f_del=0";
            strSQL += ";update TS_TR_BU_Work_Main_Resource set f_del=1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_maintype=3 and f_mainid in (select f_id from TS_TR_BU_Work_Main_Group where f_del=0 and f_bmwdid in (" + _BMWID + ")) and f_del=0";
            strSQL += ";update TS_TR_BU_Work_Main_Relation set f_del=1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_rtype in (4,5,6,7,8,9,10) and f_rid in (select f_id from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_bmwdid in (" + _BMWID + ")) and f_del=0";
            strSQL += ";update TS_TR_BU_Work_Main_Resource set f_del=1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_maintype=4 and f_mainid in (select f_id from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_bmwdid in (" + _BMWID + ")) and f_del=0";
            TS_Win_Class.MySqlCom(strSQL);
            strSQL = "insert into TS_S_T_P_Voice_Announce(f_vid,f_tablename,f_string,f_funcid,f_type,f_workerid) values(-1,'TS_TR_BU_Work_Main_Relation','" + Create_Voice_String(cb_1.Tag.ToString(), "撤回作业：  ") + "'," + cb_1.Tag.ToString() + ",2," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
            TS_Win_Class.MySqlCom(strSQL);
            _LoadStepsList();
            this.DialogResult = DialogResult.OK;
        }
    }
}